// 获取当前数据的id
var search = location.search
var arr = /id=(\d+)/.exec(search)
//判断是否登录
// if(!arr){
//     layer.msg("请登录后查看",{
//         icon:2,
//         time:1000
//     },function(){
//         location.href = 'denlu.html'
//     })
// }

var id = arr[1]
// 加载层
var loadindex = layer.load(1,{
    shade:[0.5,"#fff"]
})

// 发送请求
$.get('./php/xiangqing.php',{id},res=>{
    layer.close(loadindex)
    var {meta:{status,msg},data}=res;
    if(status === 1){
        $('.spname span').text(data.name)
        $('.spjianshao span').text(data.jianshao)
        $('.spyuanjia').text("￥"+data.yuanjia)
        $('.tab ol li').eq(0).text(data.name)
        $('.tab ol li').eq(1).text(data.jianshao)
        $('.tab ol li').eq(2).text(data.changwen)
        var bigimg = '';
        bigimg+=`
            <img src="${data.img}" alt="">
            <div class="mask"></div>
            <div class="big">
                <img src="${data.img}" alt="">
            </div>
        `
        $('.box').html(bigimg)

        var smallimg ='';
        smallimg +=`
            <img class="active" src="${data.img}" alt="">
            <img src="${data.img2}" alt="">
            <img src="${data.img3}" alt="">
        `
        $('.small').html(smallimg)
    }
    fangdajing()
    //购物车
    addcart()
},'json')
//加入购物车
function addcart(){
    $('.goumai').click(function(){
        console.log(123);
        var username = tool.getCookie('username')
        if(!username){
            layer.msg("请先登录",{
                icon:2,
                time:2000
            },function(){
                localStorage.setItem('url',location.href)
                location.href = "./denlu.html"
            })
            return false
        }

        // 判断本地存储是否有数据，有就追加，没有就新增
        var data = localStorage.getItem('data')
        if(data){
            var arr = JSON.parse(data)
            //如果有数据，筛选一下当前这条数据
            var obj = arr.find(item=>item.username === username&&item.goodsid===id)
                if(obj){
                    // 有相同数据就number加1，没有就正常添加
                    obj.number +=1
                }else{
                    var obj = {
                        goodsid:id,
                        username,
                        number:1
                    }
                     arr.push(obj)
                }
            
        } else { 
            var arr=[{
                goodsid:id,
                username,
                number:1
            }];
        }
       
        // 存储arr
        localStorage.setItem('data',JSON.stringify(arr))
    })
}





//tab
class Tab{
    constructor(classname){
        this.container = document.querySelector('.'+classname);
        this.ulis = this.container.querySelectorAll('ul li')
        this.olis = this.container.querySelectorAll('ol li')
    }
    init(){
        for(let i=0;i<this.ulis.length;i++){
            this.ulis[i].onclick = this.cut(i)
        }
    }
    cut(i){
        return ()=>{
            for(let j=0; j<this.ulis.length; j++){
                this.ulis[j].className = '';
                this.olis[j].className = '';
            }
            this.ulis[i].className = 'active';
            this.olis[i].className = 'active';
        }
    } 
}

var t = new Tab('tab')
t.init()


